/*
 * @Descripttion: 封装socket方法
 * @version:
 * @Date: 2021-08-06 11:14:39
 * @LastEditTime: 2021-10-26 14:06:34
 */
//import i18n from '@/locales'
//import store from '@/store'
//import { ElMessage } from 'element-plus'
//import { Base64 } from 'js-base64'
const language = 'store.state.users.language' // 当前用户信息，后台配置
const token = 'store.state.users.authorization' // 获取验证信息，后台配置

const socket = {
  websocket: null,
  connectURL: `${process.env.VUE_APP_SOCEKT_URL}/websocket/v1/${language}/${token}`,
  // 开启标识
  socket_open: false,
  // 心跳timer
  hearbeat_timer: null,
  // 心跳发送频率
  hearbeat_interval: 45000,
  // 是否自动重连
  is_reonnect: true,
  // 重连次数
  reconnect_count: 3,
  // 已发起重连次数
  reconnect_current: 1,
  // 网络错误提示此时
  ronnect_number: 0,
  // 重连timer
  reconnect_timer: null,
  // 重连频率
  reconnect_interval: 5000,
  init: (receiveMessage) => {
    if (!('WebSocket' in window)) {
      // ElMessage.warning('浏览器不支持WebSocket')
      console.error('浏览器不支持WebSocket')
      return null
    }
    // 已经创建过连接不再重复创建
    // if (socket.websocket) {
    //   return socket.websocket
    // }
    socket.websocket = new WebSocket(socket.connectURL)
    socket.websocket.onmessage = e => {
      receiveMessage && receiveMessage(e)
    }
    socket.websocket.onclose = () => {
      clearInterval(socket.hearbeat_interval)
      socket.socket_open = false
      // 需要重新连接
      if (socket.is_reonnect) {
        socket.reconnect_timer = setTimeout(() => {
          // 超过重连次数
          if (socket.reconnect_current > socket.reconnect_count) {
            clearTimeout(socket.reconnect_timer)
            socket.is_reonnect = false
            return
          }
          // 记录重连次数
          socket.reconnect_current++
          socket.reconnect()
        }, socket.reconnect_interval)
      }
    }
    // 连接成功
    socket.websocket.onopen = function() {
      socket.socket_open = true
      socket.is_reonnect = true
      // 开启心跳
      socket.heartbeat()
    }
    // 连接发生错误
    socket.websocket.onerror = function() {}
  },
  heartbeat: () => {
    socket.hearbeat_timer && clearInterval(socket.hearbeat_timer)

    socket.hearbeat_timer = setInterval(() => {
      let data = {
        // languageId: store.state.users.language,
        // authToken: store.state.users.authorization,
        content: 'ping',
      }
      let sendDara = {
        encryption_type: 'base64',
        data: JSON.stringify(data)
        //data: Base64.encode(JSON.stringify(data)),
      }
      socket.send(sendDara)
    }, socket.hearbeat_interval)
  },
  send: (data, callback = null) => {
    // 开启状态直接发送
    if (socket.websocket.readyState === socket.websocket.OPEN) {
      socket.websocket.send(JSON.stringify(data))
      callback && callback()
      // 正在开启状态，则等待1s后重新调用
    } else {
      clearInterval(socket.hearbeat_timer)
      if (socket.ronnect_number < 1) {
        console.error('socket链接已断开')
        // ElMessage({
        //   type: 'error',
        //   message: 'socket链接已断开',
        //   duration: 0,
        // })
      }
      socket.ronnect_number++
    }
  },
  receive: (message) => {
    let params = {msg: message} //Base64.decode(JSON.parse(message.data).data)
    params = JSON.parse(params)
    return params
  },

  close: () => {
    clearInterval(socket.hearbeat_interval)
    socket.is_reonnect = false
    socket.websocket.close()
  },
  /**
   * 重新连接
   */
  reconnect: () => {
    if (socket.websocket && !socket.is_reonnect) {
      socket.close()
    }
    socket.init(null)
  },
}
export default socket